using System.Collections;
using UnityEngine;

public class TutorialButton : MonoBehaviour
{
	public TutorialButtonId id;

	public GameObject parentObj;

	public GameObject animObj;

	public GameObject glowObj;

	private GameObject curGlowObj;

	private GameObject curAnimObj;

	public bool isPlayAnim;

	public bool isBlock;

	public bool tapComplete;

	public bool tapUpComplete;

	public bool dragComplete;

	public bool moveComplete;

	public bool startDetectDrag;

	private BoxCollider colliderButton;

	private UIDragScrollView scrollScript;

	public void Start()
	{
		if (settings.learningCompleted)
		{
			Object.Destroy(this);
		}
	}

	public void SetActive(bool val)
	{
		if (parentObj == null)
		{
			base.gameObject.SetActive(val);
		}
		else
		{
			parentObj.SetActive(val);
		}
	}

	public void StartAnimation()
	{
		BCWDebug.Log("StartAnimation =" + base.name);
		if (!(animObj != null))
		{
			return;
		}
		SetActive(true);
		curAnimObj = TutorialManager.instance.GetAnimation(animObj.name);
		curAnimObj.SetActive(true);
		isPlayAnim = true;
		if (parentObj == null)
		{
			curAnimObj.transform.parent = base.gameObject.transform;
		}
		else
		{
			curAnimObj.transform.parent = parentObj.transform;
		}
		curAnimObj.transform.localScale = Vector3.one;
		curAnimObj.transform.localPosition = Vector3.zero;
		curAnimObj.transform.parent = curAnimObj.transform.parent.parent;
		if (glowObj != null)
		{
			curGlowObj = Object.Instantiate(glowObj);
			if (parentObj == null)
			{
				curGlowObj.transform.SetParent(base.gameObject.transform);
			}
			else
			{
				curGlowObj.transform.parent = parentObj.transform;
			}
			curGlowObj.transform.localScale = Vector3.one;
			curGlowObj.transform.localPosition = Vector3.zero;
		}
	}

	public void StartAnimation(GameObject needAnim)
	{
		BCWDebug.Log("StartAnimation =" + base.name);
		if (!(needAnim != null))
		{
			return;
		}
		SetActive(true);
		curAnimObj = needAnim;
		curAnimObj.SetActive(true);
		isPlayAnim = true;
		if (parentObj == null)
		{
			curAnimObj.transform.parent = base.gameObject.transform;
		}
		else
		{
			curAnimObj.transform.parent = parentObj.transform;
		}
		curAnimObj.transform.localScale = Vector3.one;
		curAnimObj.transform.localPosition = Vector3.zero;
		curAnimObj.transform.parent = curAnimObj.transform.parent.parent;
		if (glowObj != null)
		{
			curGlowObj = Object.Instantiate(glowObj);
			if (parentObj == null)
			{
				curGlowObj.transform.SetParent(base.gameObject.transform);
			}
			else
			{
				curGlowObj.transform.parent = parentObj.transform;
			}
			curGlowObj.transform.localScale = Vector3.one;
			curGlowObj.transform.localPosition = Vector3.zero;
		}
	}

	public void StopAnimation()
	{
		if (curAnimObj != null)
		{
			isPlayAnim = false;
			TutorialManager.instance.ReturnToList(curAnimObj);
			curAnimObj.SetActive(false);
		}
		if (curGlowObj != null)
		{
			Object.Destroy(curGlowObj);
		}
	}

	private void OnPress(bool isDown)
	{
		if (isDown)
		{
			if (!tapComplete)
			{
				if (id == TutorialButtonId.shoot)
				{
					CameraZoneEnabler.GetCameraZone().GetComponent<TutorialButton>().OnPress(isDown);
				}
				TapComplete();
			}
		}
		else if (!tapUpComplete)
		{
			if (id == TutorialButtonId.shoot)
			{
				CameraZoneEnabler.GetCameraZone().GetComponent<TutorialButton>().OnPress(isDown);
			}
			TapUpComplete();
		}
	}

	private void OnDrag(Vector2 delta)
	{
		StartCoroutine(SetDrag(delta));
	}

	private IEnumerator SetDrag(Vector2 delta)
	{
		yield return new WaitForSeconds(1f);
		if (id == TutorialButtonId.shoot && CameraZoneEnabler.GetCameraZone().GetComponent<TutorialButton>().startDetectDrag)
		{
			CameraZoneEnabler.GetCameraZone().GetComponent<TutorialButton>().OnDrag(delta);
			startDetectDrag = false;
		}
		if (tapComplete && !dragComplete)
		{
			DragComplete();
		}
	}

	public void SetBlock(bool val)
	{
		BCWDebug.Log(base.gameObject.name + " SetBlock = " + true);
		if (colliderButton == null)
		{
			colliderButton = GetComponent<BoxCollider>();
		}
		if (scrollScript == null)
		{
			scrollScript = GetComponent<UIDragScrollView>();
		}
		if (isBlock && !val)
		{
			if (scrollScript != null)
			{
				scrollScript.enabled = true;
			}
			else
			{
				colliderButton.enabled = true;
			}
		}
		if (!isBlock && val)
		{
			if (scrollScript != null)
			{
				scrollScript.enabled = false;
			}
			else
			{
				colliderButton.enabled = false;
			}
		}
	}

	private void TapComplete()
	{
		tapComplete = true;
	}

	private void TapUpComplete()
	{
		tapComplete = true;
		if (dragComplete)
		{
			moveComplete = true;
		}
	}

	private void DragComplete()
	{
		dragComplete = true;
	}

	public void ResetState()
	{
		tapComplete = false;
		tapUpComplete = false;
		dragComplete = false;
		moveComplete = false;
	}
}
